package at.socialconference.web.client.ui;

import at.socialconference.inter.connection.protocol.SocketProtocol;
import at.socialconference.inter.model.Place;
import at.socialconference.web.client.SCwebService;
import at.socialconference.web.client.util.AsynCallbackInsertObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AddPlacePopup extends PopupPanel implements ClickHandler {
	private VerticalPanel content;
	private FlexTable flexTable;
	
	private TextBox txtName = new TextBox();
	private TextBox txtDescription = new TextBox();
	private TextBox txtLongitude = new TextBox();
	private TextBox txtLatitude = new TextBox();
	
	private AdminHome adminHome;
	
	private Button btnSubmit = new Button("Add");
	private Button btnCancel = new Button("Cancel");
	
	//TODO add speakers
	
	private class AsyncCallbackInsertPlace extends AsynCallbackInsertObject {
		@Override
		public void onSuccess(Integer result) {
			super.onSuccess(result);
			if (result==SocketProtocol.OK) {
				AddPlacePopup.this.adminHome.updatePlaces();
			}
		}
	}
		
	public AddPlacePopup(AdminHome adminHome) {
		this.adminHome = adminHome;
		
		content = new VerticalPanel();
		add(content);
		
		flexTable = new FlexTable();
		content.add(flexTable);
		
		int i=-1;
		
		flexTable.setWidget(++i, 0, new Label("Name:"));
		flexTable.setWidget(i, 1, txtName);
		
		flexTable.setWidget(++i, 0, new Label("Description"));
		flexTable.setWidget(i, 1, txtDescription);
			
		flexTable.setWidget(++i, 0, new Label("Longitude:"));
		flexTable.setWidget(i, 1, txtLongitude);
		
		flexTable.setWidget(++i, 0, new Label("Latitude:"));
		flexTable.setWidget(i, 1, txtLatitude);
		
		
		HorizontalPanel submitPanel = new HorizontalPanel();
		submitPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		submitPanel.add(btnSubmit);
		
		HorizontalPanel cancelPanel = new HorizontalPanel();
		submitPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		submitPanel.add(btnCancel);
		
		flexTable.setWidget(++i, 0, submitPanel);
		flexTable.setWidget(i, 1, cancelPanel);
		
		btnSubmit.addClickHandler(this);
		btnCancel.addClickHandler(this);
		
		this.setGlassEnabled(true);
		this.center();
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource().equals(btnSubmit)) {
			Place place = new Place();
			try {			
				place.setName(txtName.getText());
				place.setDescription(txtDescription.getText());
				place.setLongitude(Double.parseDouble(txtLongitude.getText()));
				place.setLatitude(Double.parseDouble(txtLatitude.getText()));
				SCwebService.Util.getInstance().insertObject(place, new AsyncCallbackInsertPlace());
				this.hide();
			} catch (NumberFormatException e) {
				Window.alert("Wrong input");
			} catch (Exception e) {
				Window.alert("Error");
			}
		}
		if (event.getSource().equals(btnCancel)) {
			this.hide();
		}
		
	}
}
